package leetcode.sort;

import java.util.Arrays;

public class BubbleSort {
    public int[] bubble_sort(int[] sourceArray) {
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        for (int i = 1; i < arr.length; i++) {
            boolean flag = true;

            for (int j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;

                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }

        return arr;
    }

    public static void main(String[] args) {
        BubbleSort bubbleSort = new BubbleSort();
        int[] arr = new int[]{1, 3, 5, 7, 3, 9, 6, 3};
        int[] ret = bubbleSort.bubble_sort(arr);
        for (int i : ret) {
            System.out.print(i + " ");
        }
    }
}
